package com.changgou.system.file.controller;

import com.changgou.system.entity.Result;
import com.changgou.system.entity.StatusCode;
import com.changgou.system.file.util.FastDFSClient;
import com.changgou.system.file.util.FastDFSFile;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FileController {

    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file) {
        try {
             if(file==null){
                 throw  new RuntimeException("文件不存在");
             }
        String originalFilename = file.getOriginalFilename();
          if(StringUtils.isEmpty(originalFilename)){
               throw  new RuntimeException("文件不存在");

          }
        //获取文件的扩展名称 abc.jpg jpg
        String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
       //获取文件内容
        byte[] content = new byte[0];

            content = file.getBytes();

        //创建文件上传的封装实体类
        FastDFSFile fastDFSFile = new FastDFSFile(originalFilename,content,extName);
        //基于工具类进行文件上传,并接受返回参数 String[]
            String[] uploadResult = FastDFSClient.upload(fastDFSFile);
           // System.out.println(uploadResult);
            String group=uploadResult[0];
            String strorePath=uploadResult[1];
            //封装返回结果
            String url= FastDFSClient.getTrackerUrl()+ group+"/"+strorePath;

            return new Result(true,StatusCode.OK,"文件上传成功",url);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new Result(false, StatusCode.ERROR,"文件上传失败");
    }
}
